﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using S7TCP;
using System.Net.Sockets;

namespace S7ClientDemo
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            Timer myTimer = new Timer();
            myTimer.Interval = 1000;
            myTimer.Tick += myTimer_Tick;
            myTimer.Enabled = true;
        }

        void myTimer_Tick(object sender, EventArgs e)
        {
            if (mSocket != null)
            {
                //读取字节数组
                byte[] Res = objS7.ReadBytes(DataType.Marker, 0, 2, 4, mSocket);
                //字节数组转成Double
                double value=S7TCP.Double.BytetoFloatByPoint(Res);
                this.seg_Data.Value = value.ToString("f2");
                this.aquaGauge1.Value = value;
                this.thermometer1.Value = value;
                this.pressureGauge1.Value = value;
            }
        }


        Socket mSocket;

        MyS7 objS7 = new MyS7();

        private void btn_Conn_Click(object sender, EventArgs e)
        {
            mSocket = objS7.Connect(this.txt_IP.Text.Trim(), CPU_Type.S7200SMART, 0, 0);
            if (mSocket != null)
            {
                MessageBox.Show("连接PLC成功", "通讯连接");
            }
            else
            {
                MessageBox.Show("连接PLC失败", "通讯连接");
            }
        }

        private void knobControl1_ValueChanged(object Sender)
        {
            objS7.Write("MD2", this.knobControl1.Value, mSocket);
        }
    }
}
